package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.dlc.commonlibrary.dialog.DoResultDialog;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.BucaoResultAdapter;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.RFIDListBean;
import com.dlc.beijingrfid.net.bean.SimpleBean;
import com.dlc.beijingrfid.net.protocol.MyProto;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;

import butterknife.BindView;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by kawaimisashi on 2017/7/19 0019.
 */

public class ChuKuDetailActivity extends BaseActivity {
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.qr_hotel)
    TextView mQrHotel;
    @BindView(R.id.qr_classify)
    TextView mQrClassify;
    @BindView(R.id.qr_pro_count)
    TextView mQrProCount;
    @BindView(R.id.differ_count)
    TextView mDifferCount;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;

    private String mHotel;
    private String mSpec;
    private String mCount;
    private ArrayList<String> mDiffs;
    private String mOrderno;

    private BucaoResultAdapter mBucaoResultAdapter;

    private int page = 1;
    private String[] rfids;
    private String pagesize = "10";

    @Override
    protected int getLayoutID() {
        return R.layout.activity_chuku_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);
        resolveIntent();

        mQrHotel.setText(mHotel);
        mQrClassify.setText(mSpec);
        mQrProCount.setText(mCount);
        mDifferCount.setText(String.valueOf(mDiffs.size()));
        InitRecyclerView();
        getOrderWashDetail();
    }

    private void resolveIntent() {
        Intent intent = getIntent();
        mOrderno = intent.getStringExtra("orderno");
        mHotel = intent.getStringExtra("hotel");
        mSpec = intent.getStringExtra("spec");
        mCount = intent.getStringExtra("count");
        mDiffs = intent.getStringArrayListExtra("diff");
        rfids = mDiffs.toArray(new String[mDiffs.size()]);
    }


    private void InitRecyclerView(){
        mBucaoResultAdapter = new BucaoResultAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mBucaoResultAdapter);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                getOrderWashDetail();
            }


        });
    }

    public void getOrderWashDetail() {

        String rfid ="" ;
        for (int i= 0; i < rfids.length;i++){
            rfid = rfid + rfids[i]+",";
        }
        rfid = rfid.substring(0,rfid.length()-1);
        MyProto.get().query_by_rfid(rfid,String.valueOf(page),pagesize)
                .compose(this.<RFIDListBean.DataBean>bindToLifecycle())
                .subscribe(new Observer<RFIDListBean.DataBean>() {
                    @Override
                    public void onCompleted() {
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefreshing();
                    }

                    @Override
                    public void onError(Throwable e) {
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefreshing();
                    }

                    @Override
                    public void onNext(RFIDListBean.DataBean dataBean) {
                        if (page == 1) {
                            mBucaoResultAdapter.setNewData(dataBean.list);
                        }else {
                            mBucaoResultAdapter.appendData(dataBean.list);
                        }
                        mBucaoResultAdapter.notifyDataSetChanged();
                    }
                });
    }
    // TODO: 2017/7/27 提交出库，然而没有执行的地方 
    private void doChuku() {

        final DoResultDialog doResultDialog = DoResultDialog.newDialog(this)
                .setSuccessImage(R.mipmap.ic_success)
                .setWorkingText("正在提交出库")
                .setSuccessText("提交成功");

        doResultDialog.setCancelable(false);

        doResultDialog.showWorking();

        MyProto.get()
                .chuku(mOrderno)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SimpleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        doResultDialog.dismiss();
                        showToast("提交失败," + e.getMessage());
                    }

                    @Override
                    public void onNext(SimpleBean simpleBean) {
                        doResultDialog.showResultAndRun(true, new Runnable() {
                            @Override
                            public void run() {
                                doResultDialog.dismiss();
                                finish();
                            }
                        }, 1000);
                    }
                });
    }
}
